/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for Task_ADC */
osThreadId_t Task_ADCHandle;
const osThreadAttr_t Task_ADC_attributes = {
  .name = "Task_ADC",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for Task_Info */
osThreadId_t Task_InfoHandle;
const osThreadAttr_t Task_Info_attributes = {
  .name = "Task_Info",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityLow,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void AppTask_ADC(void *argument);
void AppTask_Info(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of Task_ADC */
  Task_ADCHandle = osThreadNew(AppTask_ADC, NULL, &Task_ADC_attributes);

  /* creation of Task_Info */
  Task_InfoHandle = osThreadNew(AppTask_Info, NULL, &Task_Info_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_AppTask_ADC */
/**
  * @brief  Function implementing the Task_ADC thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_AppTask_ADC */
void AppTask_ADC(void *argument)
{
  /* USER CODE BEGIN AppTask_ADC */
	LCD_ShowStr(10, 40, "Task_ADC:ADC by polling");
	LCD_ShowStr(10, LCD_CurY+LCD_SP10, "ADC Value(mV)=");
	uint16_t VOLX = LCD_CurX;
	uint16_t VOLY = LCD_CurY;
	TickType_t previousWakeTime = xTaskGetTickCount();
  /* Infinite loop */
  for(;;)
  {
	  HAL_ADC_Start(&hadc1);
	  if(HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK)
	  {
		  uint32_t val=HAL_ADC_GetValue(&hadc1);
		  uint32_t volt=val*3300;
		  volt=volt>>12;
		  LCD_ShowInt(VOLX, VOLY, volt);
	  }
    vTaskDelayUntil(&previousWakeTime, pdMS_TO_TICKS(500));
  }
  /* USER CODE END AppTask_ADC */
}

/* USER CODE BEGIN Header_AppTask_Info */
/**
* @brief Function implementing the Task_Info thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_AppTask_Info */
void AppTask_Info(void *argument)
{
  /* USER CODE BEGIN AppTask_Info */
	TaskHandle_t taskHandle=Task_ADCHandle;

	TaskStatus_t taskInfo;
	BaseType_t getFreeStackSpace=pdTRUE;
	eTaskState taskState=eInvalid;
	vTaskGetInfo(taskHandle, &taskInfo, getFreeStackSpace, taskState);

	taskENTER_CRITICAL();
	LCD_ShowStr(10, 80, (uint8_t *)"Task_Info: Show task info");
	LCD_ShowStr(20, LCD_CurY+LCD_SP10, (uint8_t *)"Get by vTaskGetInfo() ");
	LCD_ShowStr(30, LCD_CurY+LCD_SP10, (uint8_t *)"Task Name= ");
	LCD_ShowStr(LCD_CurX+10, LCD_CurY, (uint8_t *)taskInfo.pcTaskName);

	LCD_ShowStr(30, LCD_CurY+LCD_SP10, (uint8_t *)"Task Number= ");
	LCD_ShowUint(LCD_CurX+10, LCD_CurY, (uint8_t *)taskInfo.xTaskNumber);

	LCD_ShowStr(30, LCD_CurY+LCD_SP10, (uint8_t *)"Task State= ");
	LCD_ShowUint(LCD_CurX+10, LCD_CurY, (uint8_t *)taskInfo.eCurrentState);

	LCD_ShowStr(30, LCD_CurY+LCD_SP10, (uint8_t *)"Task Priority= ");
	LCD_ShowUint(LCD_CurX+10, LCD_CurY, (uint8_t *)taskInfo.uxCurrentPriority);

	LCD_ShowStr(30, LCD_CurY+LCD_SP10, (uint8_t *)"Stack High Water Mark= ");
	LCD_ShowUint(LCD_CurX+10, LCD_CurY, (uint8_t *)taskInfo.usStackHighWaterMark);

	//=======用函数uxTaskGetStackHighWaterMark
	BRUSH_COLOR=LCD_GREEN;
	LCD_ShowStr(20, LCD_CurY+LCD_SP15, (uint8_t*)"High Water Mark of tasks");

	taskHandle=xTaskGetIdleTaskHandle();		//获取空闲任务句柄
	UBaseType_t hwm=uxTaskGetStackHighWaterMark(taskHandle);
	LCD_ShowStr(30, LCD_CurY+LCD_SP10, "Idle Task= ");
	LCD_ShowUint(LCD_CurX, LCD_CurY, hwm);

	taskHandle=Task_ADCHandle;
	hwm=xTaskGetHandle(taskHandle);
	LCD_ShowStr(30, LCD_CurY+LCD_SP10, "Task_ADC= ");
	LCD_ShowUint(LCD_CurX, LCD_CurY, hwm);

	taskHandle=Task_InfoHandle;
	hwm=xTaskGetHandle(taskHandle);
	LCD_ShowStr(30, LCD_CurY+LCD_SP10, "Task_Info= ");
	LCD_ShowUint(LCD_CurX, LCD_CurY, hwm);

	//===获取内核信息
	BRUSH_COLOR=LCD_BLUE;
	LCD_ShowStr(20, LCD_CurY+LCD_SP15, "Kernel Info");
	UBaseType_t taskNum=uxTaskGetNumberOfTasks();
	LCD_ShowStr(30, LCD_CurY+LCD_SP10, "Task number= ");
	LCD_ShowUint(LCD_CurX, LCD_CurY, taskNum);
	uint16_t lastRow=LCD_CurY;
	taskEXIT_CRITICAL();

	UBaseType_t loopCount=0;
  /* Infinite loop */
  for(;;)
  {
    loopCount++;
    HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_3);
    vTaskDelay(pdMS_TO_TICKS(300));
    if(loopCount==10)
    	break;
  }
  BRUSH_COLOR=LCD_RED;
  LCD_ShowStr(30, lastRow, "Task_Info is deleted");
  vTaskDelete(NULL);
  /* USER CODE END AppTask_Info */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

